;;; This example is adapted from the project website at
;;; https://dthompson.us/projects/chickadee.html.

(import
 (except (rnrs base) map)
 (only (guile)
       lambda* λ
       when
       simple-format
       current-output-port)
 ;; GNU Guile batteries
 (system repl coop-server)
 (ice-9 exceptions)
 ;; Chickadee libs
 (chickadee)
 (chickadee math vector)
 (chickadee graphics sprite)
 (chickadee graphics texture)
 (chickadee graphics font))


(define-syntax once!
  (λ (stx)
    (syntax-case stx ()
      [(once! exprs ... flag)
       (syntax
        (unless flag
          exprs ...
          (set! flag #t)))])))


(define sprite #f)
(define dt-shown #f)

(define repl (spawn-coop-repl-server))


(define load
  (λ ()
    (set! sprite (load-image "images/chickadee.png"))))


(define draw
  (λ (alpha)
    (draw-sprite sprite (vec2 256.0 176.0))))


(define update
  (λ (dt)
    (poll-coop-repl-server repl)
    (once! (simple-format (current-output-port) "dt: ~a\n" dt)
           (simple-format (current-output-port) "(elapsed-time): ~a\n" (elapsed-time))
           dt-shown)))


(run-game #:load load
          #:draw draw
          #:update update)


;;; Now you can connect to the Guile REPL server using M-x
;;; connect-to-guile in Emacs and then inspect bindings,
;;; which are already defined, modify them, or create new
;;; ones.
